/*
 * Copyright (C) 2004-2007  Autodesk, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of version 2.1 of the GNU Lesser
 * General Public License as published by the Free Software Foundation.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include "stdafx.h"
#include "FdoWcsOverrides.h"
#include "FdoWcsXmlGlobals.h"
#include <WCS/Override/FdoWcsOvRasterDefinition.h>
#include "../Provider/FdoWcsGlobals.h"

FdoWcsOvRasterDefinition::FdoWcsOvRasterDefinition(void) :
 m_pXmlContentHandler(NULL)
{
    m_coverages = FdoWcsOvCoverageCollection::Create(this);
}

FdoWcsOvRasterDefinition::~FdoWcsOvRasterDefinition(void)
{
    FDO_SAFE_RELEASE(m_pXmlContentHandler);
}

void FdoWcsOvRasterDefinition::Dispose()
{
    delete this;
}

FdoWcsOvRasterDefinition* FdoWcsOvRasterDefinition::Create()
{
    return new FdoWcsOvRasterDefinition();
}

FdoString* FdoWcsOvRasterDefinition::GetSpatialContextName(void) const
{
    return m_spatialContext;
}

void FdoWcsOvRasterDefinition::SetSpatialContextName(FdoString* value)
{
    m_spatialContext = value;
}

FdoWcsOvCoverageCollection* FdoWcsOvRasterDefinition::GetCoverages(void) const
{
    return FDO_SAFE_ADDREF(m_coverages.p);
}

void FdoWcsOvRasterDefinition::InitFromXml(FdoXmlSaxContext* pContext, FdoXmlAttributeCollection* attrs)
{
    return BaseType::InitFromXml(pContext, attrs);
}

// Handle the start of a sub-element
FdoXmlSaxHandler* FdoWcsOvRasterDefinition::XmlStartElement(
   FdoXmlSaxContext* context, 
   FdoString* uri, 
   FdoString* name, 
   FdoString* qname, 
   FdoXmlAttributeCollection* atts)
{
    FdoXmlSaxHandler* pRet = NULL;

    try 
    {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        pRet = BaseType::XmlStartElement(context, uri, name, qname, atts);
        if (pRet == NULL) {
            if (FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsCoverageDefinition) == 0) {
                FdoWcsOvCoverageDefinitionP pNewObj = FdoWcsOvCoverageDefinition::Create();
                pNewObj->InitFromXml(context, atts);
                m_coverages->Add(pNewObj);
                pRet = pNewObj;
            }
            // TODO: once we add an override-able item, we should modify below code
            else if (FdoCommonStringUtil::StringCompareNoCase(name, FdoWcsXmlGlobals::g_WcsSpatialContext) == 0) 
            {
                m_pXmlContentHandler = FdoXmlCharDataHandler::Create();
                pRet = m_pXmlContentHandler;
            }
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return pRet;
}

// Handle the end of a sub-element
FdoBoolean FdoWcsOvRasterDefinition::XmlEndElement(FdoXmlSaxContext* context, FdoString* uri, FdoString* name, FdoString* qname)
{
    try {
        VALIDATE_ARGUMENT(name);
        VALIDATE_ARGUMENT(context);

        BaseType::XmlEndElement(context, uri, name, qname);

        if (NULL != m_pXmlContentHandler)
        {
            if (FdoCommonOSUtil::wcsicmp(name, FdoWcsXmlGlobals::g_WcsSpatialContext) == 0) {
                SetSpatialContextName(m_pXmlContentHandler->GetString());
            }

            FDO_SAFE_RELEASE(m_pXmlContentHandler);
        }
    }
    catch (FdoException* ex) 
    {
        context->AddError(ex);
        ex->Release();
    }

    return false;
}

// Write this element to XML.
void FdoWcsOvRasterDefinition::_writeXml( FdoXmlWriter* xmlWriter, const FdoXmlFlags* flags )
{
    VALIDATE_ARGUMENT(xmlWriter);
    VALIDATE_ARGUMENT(flags);
    
    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsRasterDefinition);
    BaseType::_writeXml(xmlWriter, flags);

    xmlWriter->WriteStartElement(FdoWcsXmlGlobals::g_WcsSpatialContext);
    xmlWriter->WriteCharacters(GetSpatialContextName());
    xmlWriter->WriteEndElement();

    FdoInt32 count = m_coverages->GetCount();
    for (int i = 0; i < count; i++)
    {
        FdoWcsOvCoverageDefinitionP coverDefn = m_coverages->GetItem(i);
        coverDefn->_writeXml(xmlWriter, flags);
    }

    xmlWriter->WriteEndElement();
}

FdoStringP FdoWcsOvRasterDefinition::GetQualifiedName()
{
    FdoStringP ret = this->GetName ();
    FdoPtr<FdoPhysicalElementMapping> parent = this->GetParent ();
    if (parent != NULL)
    {
        FdoStringP parentQName = parent->GetQualifiedName ();
        if (parentQName.GetLength () > 0)
        {
            ret = parentQName + L"." + ret;
        }
    }

    return ret;
}
